﻿Public Partial Class Customer_Popup
    Inherits System.Web.UI.Page
    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not (Session("CUSTOMER") & "" = "") Then
                'Dim lb As Label = CType(Master.FindControl("lbMenu"), Label)
                'lb.Text = "Customer"
                lbCode.Text = Session("CUSTOMER").ToString().Split("$")(0)
                lbName.Text = Session("CUSTOMER").ToString().Split("$")(1)
                lbAddress.Text = Session("CUSTOMER").ToString().Split("$")(2)
                lbPost.Text = Session("CUSTOMER").ToString().Split("$")(3)
                If Not (radioPayin.Items.FindByValue(Session("CUSTOMER").ToString().Split("$")(4)) Is Nothing) Then
                    radioPayin.SelectedValue = Session("CUSTOMER").ToString().Split("$")(4)
                End If
                Session("CUSTOMER") = ""
            End If
            If (Session("EditMaster") Is Nothing) Then
                btnSave.Visible = False
            End If
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClose.Click
        ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " parent.closeModal();", True)
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (radioPayin.SelectedValue <> "") Then
            Dim conn As New System.Data.SqlClient.SqlConnection(connString)
            Dim clsDB As New ClassDatabase
            Dim clsFc As New ClassFunction
            Dim condition As Boolean = True
            Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
            Application.Lock()
            conn.Open()
            ' Dim ID_Customer As String = clsDB.GeneratorID(conn, "Customer")
            ' Dim ID_BillTo As String = clsDB.GeneratorID(conn, "Customer_Billto")
            tran = clsDB.BeginTran(conn)
            If (clsFc.UpdateCustomerPayin(lbCode.Text, radioPayin.SelectedValue, conn, tran) < 1) Then
                condition = False
            End If

            If (condition) Then
                clsDB.CommitTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('บันทึกข้อมูลสำเร็จ !!');parent.closeModal();", True)
            Else
                clsDB.RollBackTran(tran)
                ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('เกิดความผิดพลาดในการบันทึก !!');", True)
            End If
            conn.Close()
            Application.UnLock()
            conn.Dispose()
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('กรุณาทำการระบุวิธีการชำระเงิน !!');", True)
        End If

    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "")
        End If
    End Sub
End Class